/**************************************************************************
 * @file PD_PHY.h
 * @author 黑马小乌龟 (532352406@qq.com)
 * @brief 
 * @version 0.1
 * @date 2025-08-20
 * 
 * @copyright Copyright (c) 2025
 * 
 *************************************************************************/
#ifndef	__PD_PHY_H__
#define __PD_PHY_H__

#include "CH543.H"

 /************************
 5000  : 5V
 9000  : 9V
 12000 : 12V
 20000 : 20V
 ************************/  
//#define  Volt           5000          

#define  PowerRole      0                                    // 1:Source   0:sink
#define  Sink_VDM       0                                    //




//#define DataRole      DataRoleDFP                                                                             //


/* Headerλ���� */
#define  DataRoleUFP       0
#define  DataRoleDFP       1
#define  PwrRoleSink       0
#define  PwrRoleSource     1

#define GoodCRC       0x01
#define SourceCap     0x01  // 5B:00001
#define Request       0x02  // 5B:00010
#define Accept        0x03  // 5B:00011
#define Reject        0x04  // 5B:00100
#define PS_RDY        0x06  // 5B:00110
#define GetSrcCap     0x07  // 5B:00111
#define GetSinkCap    0x08
#define DR_Swap       0x09  // 5B:01001
#define PR_Swap       0x0A  // 5B:01010
#define SoftRst       0x0D  // 5B:01101
#define NotSup        0x10  // 5B:10000
#define SourceCap_VDM 0x0F

#define DufaultPowerMin (342 - 137)
#define DufaultPowerMax (342 + 158)
#define Power1_5AMin    (771 - 198)
#define Power1_5AMax    (771 + 180)
#define Power3_0AMin    (1383 - 310)
#define Power3_0AMax    (1383 + 315)

typedef struct {
    UINT8 SendingACK_DP_Stat_Pos_1 : 1;
    UINT8 SendingACK_DP_Configure_Pos_1 : 1;
    UINT8 SendingACK_Enter_Mode : 1;
    UINT8 SendingACK_Disc_Mode : 1;
    UINT8 SendingACK_Disc_SVID : 1;
    UINT8 SendingACK_Disc_Ident : 1;
    UINT8 SendingSinkCap : 1;
    UINT8 SendingVDM : 1;

    UINT8 SendingPS_RDY : 1;
    UINT8 SendingAccept : 1;
    UINT8 SendingSourceCap : 1;
    UINT8 SendingRequest : 1;
    UINT8 SendingGoodCRC : 1;
    UINT8 WaitingGoodCRC : 1;
    UINT8 RetryCnt : 2;
} _PD_PHY_STAT;

typedef union {				/* Header */
	struct _Header_Struct {
			
		UINT8	MsgType:5;
		UINT8	DataRole:1;		// 0:UFP  1:DFP
		UINT8	SpecRev:2;			// 01:Rev2.0
		
		UINT8	PwrRole:1;		// 0:Sink  1:Source
		UINT8	MsgID:3;
		UINT8	NDO:3;
		UINT8	Extended:1;
		
	} Struct;
	UINT16		Data;
} _Union_Header_;

typedef union 
{				
    /* Header */
	struct _Fixed_Struct {
			
		UINT8	CurrentL8:8;		// Maximum Current in 10mA units  
		
		UINT8	CurrentH2:2;		// 
		UINT8	VoltageL6:6;			// Voltage in 50mV units 
		
		UINT8	VoltageH4:4;			// 01:Rev2.0
		UINT8	PeakCurrent:2;
		UINT8	Reserved:1;		// 0:UFP  1:DFP
		UINT8	EPRMode:1;
		
		UINT8	UnchunkedExtMsg:1;
		UINT8	DualRoleData:1;		// 0:UFP  1:DFP
		UINT8	USBComm:1;
		UINT8	UnconstrainedPower:1;		// 0:Sink  1:Source
		UINT8	USBSuspend:1;
		UINT8	DualRolePwr:1;
		UINT8	Type:2;
		
	} Struct;
	UINT32		Data;
} _Union_Fixed;


/* Header */
typedef struct  {
//	Extended	  1		0
//	NDO			  3
//	MsgID		  3
//	PortPwrRole	  1		0:Sink  1:Source
//	SpecRev		  2		01 Rev2.0
//	PortDataRole  1		0:UFP  1:DFP
//	MessageType	  5

	UINT8 MsgType:5;      //a1 : buff[0]
	UINT8 PortDataRole:1;
	UINT8 SpecRev:2;
	//a1 71 
	//  1010 0001 0111 0001 
	UINT8 PortPwrRole:1;   //71   : buff[1]
	UINT8 MsgID:3;
	UINT8 NDO:3;
	UINT8 Extended:1;
} _Msg_Header_Struct;
typedef union {				                                              /* Header */
	_Msg_Header_Struct  HeaderStruct;
	UINT8 HeaderData[2];
} _Union_Header;

/*Table 6.7 “Power Data Object Type”
    B31…30 
    00b Fixed supply (Vmin = Vmax) 
    01b Battery 
    10b Variable Supply (non-Battery) 
    11b Augmented Power Data Object (APDO)
*/

typedef enum{
    PDOT_FIXED_SUPPLY=0,//00b Fixed supply (Vmin = Vmax) 
    PDOT_BATTERY,//01b Battery 
    PDO_VARIABLE_SUPOLY,//10b Variable Supply (non-Battery) 
    AUGMENTED_POWER_DATA_OBJECT,//11b Augmented Power Data Object (APDO)
}PowerDataObjectTypeT;

typedef struct {
    //	Data	12bit
    //	Volt	10bit		Voltage *0.05V
    //	Curr	10bit		Current *0.01A
    UINT8 CurrentL : 8;  // B9…0 Maximum Current in 10mA units
    UINT8 CurrentH : 2;
    UINT8 VoltL : 6;  // B19…10 Voltage in 50mV units
    UINT8 VoltH : 4;
    UINT8 peak_current : 2;           // B21…20 Peak Current
    UINT8 reserved_bit : 1;           // B22 Reserved – Shall be set to zero.
    UINT8 epr_mode_cap : 1;           // B23 EPR Mode Capable
    UINT8 UEMS : 1;                   // B24 Unchunked Extended Messages Supported
    UINT8 dual_rol_data : 1;          // B25 Dual-Role Data
    UINT8 usb_com_capable : 1;        // B26 USB Communications Capable
    UINT8 unconstrained_power : 1;    // B27 Unconstrained Power
    UINT8 usb_suspend_supported : 1;  // B28 USB Suspend Supported
    UINT8 dual_role_power : 1;        // B29 Dual-Role Power
    UINT8 pdo_type : 2;               // B31…30 PDO Type

} fixed_supply_pdo_source;

/*SPR Programmable Power Supply APDO – Source
    B6…0 Maximum Current in 50mA increments
    B7 Reserved – Shall be set to zero
    B15…8 Minimum Voltage in 100mV increments
    B16 Reserved – Shall be set to zero
    B24…17 Maximum Voltage in 100mV increments
    B26…25 Reserved – Shall be set to zero 
    B27 PPS Power Limited 
    B29…28 00b – SPR Programmable Power Supply 
    B31…30 11b – Augmented Power Data Object (APDO) 
*/

typedef union {				                                              /* Src Cap */
	fixed_supply_pdo_source  fixed_pdo;
	UINT8 SrcCapData[4];
} _Union_SrcCap;

/**************************************************************************
 * 
 * B31…28 Object position (0000b and 1110b…1111b are Reserved and Shall Not be used) 
 * B27 GiveBack flag = 0
 * B26 Capability Mismatch 
 * B25 USB Communications Capable 
 * B24 No USB Suspend 
 * B23 Unchunked Extended Messages Supported 
 * B22 EPR Mode Capable 
 * B21…20 Reserved - Shall be set to zero. 
 * B19…10 Operating current in 10mA units 
 * B9…0 Maximum Operating Current 10mA units
 * 
 *************************************************************************/
typedef struct{
    UINT8 max_op_curr_l:8;//B9…0 Maximum Operating Current 10mA units
    UINT8 max_op_curr_h:2;

    UINT8 op_curr_l:6;//B19…10 Operating current in 10mA units 
    UINT8 op_curr_h:4;

    UINT8 Reserved:2;//B21…20 Reserved - Shall be set to zero. 
    UINT8 epr_m_capable:1;//B22 EPR Mode Capable 
    UINT8 unch_ext_msg_supported:1;// B23 Unchunked Extended Messages Supported
    UINT8 no_usb_suspend:1;// B24 No USB Suspend 
    UINT8 usb_comm_capable:1;// B25 USB Communications Capable 
    UINT8 cap_mismatch:1;// B26 Capability Mismatch 
    UINT8 give_back_flg:1;// B27 GiveBack flag = 0
    UINT8 obj_possition:4;// B31…28 Object position (0000b and 1110b…1111b are Reserved and Shall Not be used) 
}ReqSrcCapDatsT;
typedef union {				                                             
	ReqSrcCapDatsT  req_cap;
	UINT8 req_src_cap[4];
}Request_SrcCap;

/// VDM Header                        
typedef struct  {
//	SVID			16		0xFF01: DisplayPort
//
//	StructuredVDM	1
//	SVDMVer			2		00: Ver.1
//	----			5
//	CommandType		2		00: REQ		01: ACK
//	----			1
//	Command			5
	
	// 01 80  
//	0000 0001 1000 0000 
	UINT16 ModeIndex:3;
	UINT16 :2;
	UINT16 SVDMVer:2;
	UINT16 StructuredVDM:1;
	
	UINT16 Command:5;
	UINT16 :1;
	UINT16 CommandType:2;


  UINT16 SVIDH8:8; //00 ff
  UINT16 SVIDL8:8;
} _VDM_Hdr_Struct;
typedef union {
	_VDM_Hdr_Struct  VDMHdrStruct;
	UINT8 VDMHdrData[4];
} _Union_VDM_Hdr;

/* �ⲿ���ñ��� */
                                                 
extern _Union_Header  xdata *Union_Header;                                //ָ��Header��ָ��
extern _Union_SrcCap  xdata *Union_SrcCap;                                //ָ���ѹ�����Խṹ
extern _Union_VDM_Hdr xdata *Union_VDM_Hdr;                              //VDMͷ


extern UINT8X CCSel; 
extern UINT8X MsgID;




void pd_phy_init(void);
void Timer0Init(void);
void mTimer_x_SetData(UINT16 dat);

void usr_pd_port_isr(void);
UINT8 Check_CC_Connect(void);
UINT8 Check_Dev_Remove(void);

void pd_phy_h_reset(void);
void pd_phy_send_good_crc(void);
void pd_phy_send_req_capacity(void);


void pd_phy_rx_init(void);
void pd_phy_tx_init(void);



void pd_phy_send_get_src_capacity_msg(void);

void pd_phy_send_sink_capacity_msg(void);


#endif

